﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iMathMvc4.vn.chuyentin.imathservice;

namespace iMathMvc4.Controllers
{
    public class QuestionController : Controller
    {
        //
        // GET: /Exam/
        private vn.chuyentin.service.iUserService userService = new vn.chuyentin.service.iUserService();
        private vn.chuyentin.imathservice.iMathService mathService = new vn.chuyentin.imathservice.iMathService();

        public ActionResult Index()
        {
            List<QuestionView> _list = new List<QuestionView>();
            //Lấy Exam đầu tiên
            Exam _exam = mathService.GetExams().FirstOrDefault();
            //Lấy danh sách các câu hỏi theo Exam
            _list = mathService.GetQuestionsByExam(_exam.Id).ToList();

            //_list = mathService.GetAllQuestions();
            return View("Index", _list);
        }

        //
        // GET: /Exam/Details/5

        public ActionResult Details(int id)
        {
            Question _question = new Question();
            _question = mathService.GetQuestion(id);

            return View("Details", _question);
        }

        //
        // GET: /Exam/Create

        public ActionResult Create(int examId)
        {
            //Lưu Id Exam khi người dùng click vào link Add Question
            Session.Add("ExamId", examId);
            return View();
        }

        //
        // POST: /Exam/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {                        
            try
            {
                //chuyển examId từ kiểu oject sang int
                int _examId = int.Parse(Session["ExamId"].ToString());
                string Title = collection["Title"].ToString();
                string Content = collection["Content"].ToString();

                var _question = mathService.PostQuestionWithExam("taipm", "P@$$w0rd", Title, Content, _examId);

                if (_question != null)
                    return RedirectToAction("Details", _question);
                else
                    return RedirectToAction("Error");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Exam/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Exam/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Exam/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Exam/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
